package com.yizhi.student.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.yizhi.common.annotation.Log;
import com.yizhi.common.controller.BaseController;
import com.yizhi.common.utils.*;
import com.yizhi.student.domain.ClassDO;
import com.yizhi.student.service.ClassService;
import com.yizhi.student.service.CollegeService;
import com.yizhi.student.service.MajorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Mapper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.yizhi.student.domain.StudentInfoDO;
import com.yizhi.student.service.StudentInfoService;

/**
 * 学生基础信息表
 */
@Slf4j
@Controller
@RequestMapping("/student/studentInfo")
public class StudentInfoController extends BaseController {


    @Autowired
    private StudentInfoService studentInfoService;

    @Log("查询指定id学生信息")
    @GetMapping("get/{id}")
    public String get(@PathVariable("id") Integer id) {
        return studentInfoService.get(id).toString();
    }

    /**
     * save
     *
     * @param studentInfoDO
     * @return
     */
    @Log("学生信息保存")
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("student:studentInfo:add")
    public R save(StudentInfoDO studentInfoDO) {
        Long userId = getUserId();
        studentInfoDO.setAddUserid(Math.toIntExact(userId));
        log.info("获取到userid为" + userId);
        log.info("*******************************" + studentInfoDO.toString() + "*******************************");
        int isOk = studentInfoService.save(studentInfoDO);
        if (isOk > 0) {
            return R.ok("添加成功");
        } else {
            return R.error("添加失败");
        }
    }

    /**
     * 还不会 可分页 查询
     */
    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("student:studentInfo:studentInfo")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        if (params.get("sort")!=null) {
            // 将sort的值改成驼峰命名
            params.put("sort",BeanHump.camelToUnderline(params.get("sort").toString()));
        }
        Query query = new Query(params);
        log.info("+++++++++++++++++++++++");
        log.info(query.toString());
        log.info("+++++++++++++++++++++++");
        List<StudentInfoDO> studentInfoDOList = studentInfoService.list(query);
        PageUtils pageUtils = new PageUtils(studentInfoDOList, studentInfoService.count(query),query.getCurrPage(),query.getPageSize());
        return pageUtils;

    }


    /**
     * 修改
     */
    @Log("学生基础信息表修改")
    @ResponseBody
    @PostMapping("/update")
    @RequiresPermissions("student:studentInfo:edit")
    public R update(StudentInfoDO studentInfo) {
        Long userId = getUserId();
        studentInfo.setEditUserid(Math.toIntExact(userId));
        log.info("获取到userid为:" + userId);
        int isOk = studentInfoService.update(studentInfo);
        if (isOk == 1) {
            return R.ok("更新成功");
        } else {
            return R.error("更新失败");
        }
    }

    /**
     * 删除
     */
    @Log("学生基础信息表删除")
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("student:studentInfo:remove")
    public R remove(Integer id) {
        int isOk = studentInfoService.remove(id);
        log.info("删除的返回值是：" + isOk);
        if (isOk == 1) {
            return R.ok("删除成功");
        } else {
            return R.error("删除失败");
        }
    }

    /**
     * 批量删除
     */
    @Log("学生基础信息表批量删除")
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("student:studentInfo:batchRemove")
    public R remove(@RequestParam("ids[]") Integer[] ids) {
        int isOk = studentInfoService.batchRemove(ids);
        log.info("批量删除的返回值是：" + isOk);
        if (isOk != 0) {
            return R.ok("批量删除成功");
        } else {
            return R.error("批量删除失败");
        }
    }


    //前后端不分离 客户端 -> 控制器-> 定位视图

    /**
     * 学生管理 点击Tab标签 forward页面
     */
    @GetMapping()
    @RequiresPermissions("student:studentInfo:studentInfo")
    String StudentInfo() {
        return "student/studentInfo/studentInfo";
    }

    /**
     * 更新功能 弹出View定位
     */
    @GetMapping("/edit/{id}")
    @RequiresPermissions("student:studentInfo:edit")
    String edit(@PathVariable("id") Integer id, Model model) {
        StudentInfoDO studentInfo = studentInfoService.get(id);
        model.addAttribute("studentInfo", studentInfo);
        return "student/studentInfo/edit";
    }

    /**
     * 学生管理 添加学生弹出 View
     */
    @GetMapping("/add")
    @RequiresPermissions("student:studentInfo:add")
    String add() {
        return "student/studentInfo/add";
    }

}//end class
